package com.dflc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dflc.common.CommonConstant;
import com.dflc.entity.po.SysMenu;
import com.dflc.entity.po.SysRole;
import com.dflc.entity.po.SysRoleMenu;
import com.dflc.entity.po.SysUserRole;
import com.dflc.mapper.SysMenuMapper;
import com.dflc.service.*;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
* @author 86158
* @description 针对表【sys_menu】的数据库操作Service实现
* @createDate 2025-03-05 10:55:59
*/
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu>
    implements SysMenuService{
    @Resource
    private SysRoleMenuService sysRoleMenuService;
    @Resource
    private SysUserRoleService sysUserRoleService;
    @Resource
    private UUnitService uUnitService;
    @Resource
    private SysRoleService sysRoleService;
    /**
     * 构造菜单树
     * @param sysMenuList
     * @return
     */
    public List<SysMenu> buildTreeMenu(List<SysMenu> sysMenuList){
        List<SysMenu> resultMenuList = new ArrayList<>();

        for (SysMenu sysMenu : sysMenuList) {
            // 寻找子节点
            for (SysMenu e : sysMenuList) {
                if (e.getParentId()==sysMenu.getId()) {
                    sysMenu.getChildren().add(e);
                }
            }
            sysMenu.getChildren().sort(Comparator.comparing(SysMenu::getOrderNum));
            // 判断父节点，添加到集合
            if(sysMenu.getParentId()==0L){
                resultMenuList.add(sysMenu);
            }
        }
        resultMenuList.sort(Comparator.comparing(SysMenu::getOrderNum));
        return resultMenuList;
    }

    @Override
    public  List<SysMenu> getMenuByRoleIds(List<Integer> integerList1) {
        List<SysRoleMenu> roleMenuList = sysRoleMenuService.list(new QueryWrapper<SysRoleMenu>().eq("role_id",integerList1.get(0))
                .eq("type", CommonConstant.RoleMenuType.All));
        List<Long> menuIdList = roleMenuList.stream().map(p -> p.getMenuId()).collect(Collectors.toList());
        if (ObjectUtils.isEmpty(menuIdList)  || menuIdList.size()==0){
            List<SysMenu> sysMenuList = new ArrayList<>();
            return sysMenuList;
        }
        List<SysMenu> sysMenuList = this.list(new QueryWrapper<SysMenu>().in("id",menuIdList)
                .eq("state", CommonConstant.StateActive.Active));
//        Map<String,Object> map=new HashMap<>();
//        map.put("sysMenuList",sysMenuList);
        return sysMenuList;
    }

    @Override
    public Map<String,Object> getMenuByUserIds(List<Integer> integerList1) {

        List<SysUserRole> sysUserRoles = sysUserRoleService.list(new QueryWrapper<SysUserRole>().in("user_id",integerList1));
        Set<String> permissionList = new HashSet<>();
        Set<String> permissionRoleList = new HashSet<>();
        for (SysUserRole sysUserRole : sysUserRoles){
            //获取角色信息
            SysRole sysRole = sysRoleService.getById(sysUserRole.getRoleId());
            //每个角色进行组装集合
            List<SysRoleMenu> roleMenuList = sysRoleMenuService.list(new QueryWrapper<SysRoleMenu>().eq("role_id",sysUserRole.getRoleId()));
            if (ObjectUtils.isEmpty(roleMenuList) && roleMenuList.size()==0){
                continue;
            }
            List<Long> menuIdList = roleMenuList.stream().map(p -> p.getMenuId()).collect(Collectors.toList());

            List<SysMenu> sysMenuList = this.list(new QueryWrapper<SysMenu>().in("id",menuIdList).eq("state", CommonConstant.StateActive.Active)
                    .and(wrapper -> wrapper.eq("menu_type", CommonConstant.MenuType.F).or().eq("menu_type", CommonConstant.MenuType.C)));
            for (SysMenu sysMenu : sysMenuList){
                String  str = sysMenu.getPerms();
                if (!permissionList.contains(str)){
                    permissionList.add(str);
                }
                //菜单和角色数组
                String  str2 = sysMenu.getPerms()+":"+sysRole.getCode();
                if (!permissionRoleList.contains(str2)){
                    permissionRoleList.add(str2);
                }
            }
        }
        List<String> permissionList2 = new ArrayList<>();
        for (String str : permissionList){
            permissionList2.add(str);
        }
        List<String> permissionList3 = new ArrayList<>();
        for (String str : permissionRoleList){
            permissionList3.add(str);
        }
        Map<String,Object> map=new HashMap<>();
        map.put("sysMenuList",permissionList2);
        map.put("sysMenuRoleList",permissionList3);
        return map;
    }



    @Override
    public List<SysMenu> getMenuByUserIds2(List<Integer> integerList1) {
        List<SysMenu> sysMenuList = new ArrayList<>();
        List<SysUserRole> sysUserRoles = sysUserRoleService.list(new QueryWrapper<SysUserRole>().in("user_id",integerList1));
        if (ObjectUtils.isEmpty(sysUserRoles)  || sysUserRoles.size()==0){
            return sysMenuList;
        }
        List<Long> roleIds = sysUserRoles.stream().map(p -> p.getRoleId()).collect(Collectors.toList());
        //每个角色进行组装集合
        List<SysRoleMenu> roleMenuList = sysRoleMenuService.list(new QueryWrapper<SysRoleMenu>().in("role_id",roleIds));
        if (ObjectUtils.isEmpty(roleMenuList)  || roleMenuList.size()==0){
            return sysMenuList;
        }
        List<Long> menuIdList = roleMenuList.stream().map(p -> p.getMenuId()).collect(Collectors.toList());
        sysMenuList = this.list(new QueryWrapper<SysMenu>().in("id",menuIdList).eq("state", CommonConstant.StateActive.Active)
                .and(wrapper -> wrapper.eq("menu_type", CommonConstant.MenuType.M).or().eq("menu_type", CommonConstant.MenuType.C)));
        return buildTreeMenu(sysMenuList);
    }

    @Override
    public List<SysMenu> getMenuByUserIds3() {
        List<SysMenu> sysMenuList = this.list(new QueryWrapper<SysMenu>().eq("state", CommonConstant.StateActive.Active)
                .and(wrapper -> wrapper.eq("menu_type", CommonConstant.MenuType.M).or().eq("menu_type", CommonConstant.MenuType.C)));
        return buildTreeMenu(sysMenuList);
    }

}




